package xyz.yuzegod.megawalls.classes.normal.pirate;

import net.minecraft.server.v1_8_R3.Entity;
import net.minecraft.server.v1_8_R3.World;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.nms.CustomCannonball;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class MainSkill extends Skill
{
    public MainSkill(final Classes classes) {
        super("加农炮火", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 4.0;
            }
            case 2: {
                return 5.0;
            }
            case 3: {
                return 6.0;
            }
            case 4: {
                return 7.0;
            }
            case 5: {
                return 8.0;
            }
            default: {
                return 4.0;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7发射炮弹,击中时造成§a" + this.getAttribute(level) + "§7的伤害,");
            lore.add("   §7在爆炸前对每个玩家造成一半伤害,");
            lore.add("   §7并击退附近的玩家。");
            return lore;
        }
        lore.add(" §8▪ §7发射炮弹,击中时造成§8" + this.getAttribute(level - 1) + " ➜ §a" + this.getAttribute(level) + "§7的伤害,");
        lore.add("   §7在爆炸前对每个玩家造成一半伤害,");
        lore.add("   §7并击退附近的玩家。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final Player player = gamePlayer.getPlayer();
        final Location location = player.getEyeLocation();
        final Vector direction = location.getDirection().multiply(10);
        final CustomCannonball cannonball = new CustomCannonball((World)((CraftWorld)Bukkit.getWorld("world")).getHandle(), direction.getX(), direction.getY() - 1.0, direction.getZ());
        cannonball.setShooter(player);
        cannonball.setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
        cannonball.getBukkitEntity().setMetadata(MegaWalls.getMetadataValue(), (MetadataValue)new FixedMetadataValue((Plugin)MegaWalls.getInstance(), (Object)gamePlayer.getGameTeam()));
        ((CraftWorld)Bukkit.getWorld("world")).getHandle().addEntity((Entity)cannonball, CreatureSpawnEvent.SpawnReason.CUSTOM);
        new BukkitRunnable() {
            public void run() {
                if (cannonball.getBukkitEntity().isDead() || cannonball.getBukkitEntity().isOnGround()) {
                    ParticleEffect.EXPLOSION_HUGE.display(0.0f, 0.0f, 0.0f, 1.0f, 1, cannonball.getBukkitEntity().getLocation(), 30.0);
                    cannonball.getBukkitEntity().getWorld().playSound(cannonball.getBukkitEntity().getLocation(), Sound.EXPLODE, 1.0f, 0.0f);
                    final int level = kitStats.getSkillLevel();
                    for (final Player player1 : PlayerUtils.getNearbyPlayers(cannonball.getBukkitEntity().getLocation(), 10.0)) {
                        final GamePlayer gamePlayer1 = GamePlayer.get(player1.getUniqueId());
                        if (!gamePlayer1.isSpectator()) {
                            if (gamePlayer.getGameTeam().isInTeam(gamePlayer1)) {
                                continue;
                            }
                            player1.damage(MainSkill.this.getAttribute(level), (org.bukkit.entity.Entity)player);
                        }
                    }
                    if (!cannonball.getBukkitEntity().isDead()) {
                        cannonball.getBukkitEntity().remove();
                    }
                    this.cancel();
                    return;
                }
                ParticleEffect.LAVA.display(0.0f, 0.0f, 0.0f, 1.0f, 5, cannonball.getBukkitEntity().getLocation(), 30.0);
            }
        }.runTaskTimer((Plugin)MegaWalls.getInstance(), 0L, 1L);
        return true;
    }
}
